<?php

class FP_View_Helper_AppendCss {

   public function setView(Zend_View_Interface $pView){
      $this->view = $pView;
   }

   public function appendCss($pCss=null, $pModule=null){
      if (Zend_Registry::isRegistered('assets_css___'))
	 $css = Zend_Registry::get('assets_css___');
      else
	 $css = array();

      if ($pCss){
	 if (!$pModule)
	    $pModule = 'default';

	 if (array_key_exists($pModule, $css))
	    $module = $css[$pModule];
	 else
	    $module = array();


	 if (is_array($pCss))
	    $module = array_merge($pCss, $module);
	 else {
	    if (!in_array($pCss, $module))
	       array_unshift($module, $pCss);
	 }

	 $css[$pModule] = $module;

	 Zend_Registry::set('assets_css___', $css);
      } else {

	if (FP_Application::getStagingEnvironment() == STAGING_DEVEL){
	    $prevent_cache = '&' . time();
	} else
	    $prevent_cache = '';

	 foreach ($css as $module => $assets){
	    if ($module == 'default')
	       return "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$this->view->assets}/styles/css.php?include=" . implode(',', $assets) . "{$prevent_cache}\"/>";
	    else
	       return "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$this->view->assets}/$module/styles/css.php?include=" . implode(',', $assets) . "{$prevent_cache}\"/>";
	 }
      }


   }

}

?>